<template>
    <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog" style="width: 50%;">
        <el-form :inline="true" :model="addFaProjectForm" ref="addFaProjectRef" :rules="addFaProjectrules"
            v-if="this.title == '组合拆分'">
            <el-form-item label="资产数量" :label-width="formLabelWidth" prop="">
                <el-input v-model="chaiFenData.amount" autocomplete="off" ref="name" placeholder="请填写资产数量"
                    disabled></el-input>
            </el-form-item>
            <el-form-item label="拆分价值" :label-width="formLabelWidth" prop="">
                <el-input v-model="chaiFenData.money" autocomplete="off" ref="name" placeholder="请填写拆分价值"
                    disabled></el-input>
            </el-form-item>
            <el-form-item label="拆分数量" :label-width="formLabelWidth" prop="">
                <el-input v-model="amount1" autocomplete="off" ref="name" placeholder="请填写拆分数量"
                    @change="changeAmount"></el-input>
            </el-form-item>
            <el-form-item label="拆分金额" :label-width="formLabelWidth" prop="">
                <el-input v-model="money1" autocomplete="off" ref="name" placeholder="请填写拆分金额"
                    @change="changeMoney"></el-input>
            </el-form-item>
            <el-form-item label="剩余数量" :label-width="formLabelWidth" prop="">
                <el-input v-model="minusAmount1" autocomplete="off" ref="name" placeholder="自动生成" disabled></el-input>
            </el-form-item>
            <el-form-item label="剩余金额" :label-width="formLabelWidth" prop="">
                <el-input v-model="minusMoney1" autocomplete="off" ref="name" placeholder="自动生成" disabled></el-input>
            </el-form-item>
        </el-form>
        <el-form :inline="true" :model="addFaProjectForm" ref="addFaProjectRef" :rules="addFaProjectrules"
            v-if="this.title == '平均拆分'">
            <el-form-item label="资产数量" :label-width="formLabelWidth" prop="">
                <el-input v-model="chaiFenData.amount" autocomplete="off" ref="name" placeholder="请填写资产数量"
                    disabled></el-input>
            </el-form-item>
            <el-form-item label="拆分价值" :label-width="formLabelWidth" prop="">
                <el-input v-model="chaiFenData.money" autocomplete="off" ref="name" placeholder="请填写拆分价值"
                    disabled></el-input>
            </el-form-item>
            <el-form-item label="拆分数量" :label-width="formLabelWidth" prop="">
                <el-input v-model="chaiFenData.amount" autocomplete="off" ref="name" placeholder="请填写拆分数量"
                    disabled></el-input>
            </el-form-item>
            <el-form-item label="拆分单价" :label-width="formLabelWidth" prop="">
                <el-input v-model="chaiFenData.price" autocomplete="off" ref="name" placeholder="请填写拆分单价"
                    disabled></el-input>
            </el-form-item>
        </el-form>



        <template #footer>
            <span class="dialog-footer">
                <el-button @click="cancel()">关闭</el-button>
                <el-button type="primary" @click="save()">拆分</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script>
import { ref } from "vue";
import { httpService } from "@/utils/httpService";
import { ElMessage, ElForm } from "element-plus";
import { vxRule } from "@/utils/validator";

export default {
    name: "AddFaAssets",
    components: { ElForm, ElMessage },
    data() {
        return {
            dialogFormVisible: ref(false),
            formLabelWidth: "120px",
            title: "",
            name: '',
            personnelType: "",
            rowIndex: '',
            columnIndex: '',
            parentId: '',
            excelAccept: '.xls,.xlsx',
            addEditFaAssets: {},
            // 校验规则
            addEditFaAssetsrules: {
                // company: vxRule(true, null, "blur"),
            },
            list: [],
            chaiFenData: [],
            amount1: '',
            money1: '',
            minusAmount1: '',
            minusMoney1: '',
            oneAmount1: '',
            oneMoney1: '',
            chaiFenForm: {

            },
        };
    },
    methods: {
        closeDialog() {
            // this.clear();
            this.list = []
            this.clear()
        },
        cancel() {
            this.dialogFormVisible = false;
            this.amount1 = ''
            this.money1 = ''
            this.minusAmount1 = ''
            this.minusMoney1 = ''
        },


        clear() {
            this.dialogFormVisible = false;
            this.amount1 = ''
            this.money1 = ''
            this.minusAmount1 = ''
            this.minusMoney1 = ''
        },
        changeAmount(value) {
            console.log("数量", value);
            if (this.chaiFenData.amount < this.amount1) {
                ElMessage.error("拆分数量大于资产数量，请重写");
                this.amount1 = ''
            } else {
                this.minusAmount1 = this.chaiFenData.amount - this.amount1
            }
        },
        changeMoney(value) {
            console.log("金额", value);
            if (this.chaiFenData.money < this.money1) {
                ElMessage.error("拆分金额大于资产金额，请重写");
                this.money1 = ''
            } else {
                this.minusMoney1 = this.chaiFenData.money - this.money1
            }

        },
        save() {
            console.log(this.chaiFenData);
            console.log(this.amount1, this.money1, this.title == '组合拆分');
            if (this.name == '') {

                if (this.title == '组合拆分') {
                    this.minusAmount1 = this.chaiFenData.amount - this.amount1
                    this.minusMoney1 = this.chaiFenData.money - this.money1
                    httpService(`/javaApi/faList/groupChaifen`, {
                        'amount1': this.amount1,
                        'money1': this.money1,
                        'id': this.chaiFenData.id,
                    }, "get").then(
                        (data) => {
                            // console.log(data);
                            if (data.code == 200) {
                                ElMessage.success("拆分成功");
                                this.dialogFormVisible = false;
                                this.minusAmount1 = ''
                                this.minusMoney1 = ''
                                this.$parent.getFaProjectList();
                            } else {
                                ElMessage.error("拆分失败");
                            }
                        }
                    );
                } else {
                    httpService(`/javaApi/faList/chaifen`, {
                        'id': this.chaiFenData.id,
                    }, "get").then(
                        (data) => {
                            // console.log(data);
                            if (data.code == 200) {
                                ElMessage.success("拆分成功");
                                this.dialogFormVisible = false;
                                this.$parent.getFaProjectList();
                            } else {
                                ElMessage.error("拆分失败");
                            }
                        }
                    );
                }
            } else {
                if (this.title == '组合拆分') {
                    this.minusAmount1 = this.chaiFenData.amount - this.amount1
                    this.minusMoney1 = this.chaiFenData.money - this.money1
                    httpService(`/javaApi/faSubList/groupChaifen`, {
                        'amount1': this.amount1,
                        'money1': this.money1,
                        'id': this.chaiFenData.id,
                    }, "get").then(
                        (data) => {
                            // console.log(data);
                            if (data.code == 200) {
                                ElMessage.success("拆分成功");
                                this.dialogFormVisible = false;
                                this.minusAmount1 = ''
                                this.minusMoney1 = ''
                                this.$parent.getFaProjectList();
                            } else {
                                ElMessage.error("拆分失败");
                            }
                        }
                    );
                } else {
                    httpService(`/javaApi/faSubList/chaifen`, {
                        'id': this.chaiFenData.id,
                    }, "get").then(
                        (data) => {
                            // console.log(data);
                            if (data.code == 200) {
                                ElMessage.success("拆分成功");
                                this.dialogFormVisible = false;
                                this.$parent.getFaProjectList();
                            } else {
                                ElMessage.error("拆分失败");
                            }
                        }
                    );
                }
            }


        },
    },
};
</script>
<style>
.el-dialog {
    width: 50%;
}

.selest_11 {
    width: 190px;
}

.el-upload button {
    width: 190px;
}
.el-dialog__header {
    padding: var(--el-dialog-padding-primary);
    padding-bottom: 10px;
    border-bottom: 1px solid #dedede;
    margin-bottom: 15px;
}
</style>